<script>
import {
  Scene,
  PerspectiveCamera,
  WebGLRenderer,
  Vector3,
  CubeGeometry,
  MeshLambertMaterial,
  Mesh,
  AmbientLight
} from 'three'

import Detector from 'three/examples/js/Detector'
export default {
  template: '<div style="width: 100%; height: 100%"></div>',
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      cube: null,
      ele: null
    }
  },
  mounted() {
    this.ele = this.$el

    if (Detector.webgl) {
      this.init()
    } else {
      const warning = Detector.getWebGLErrorMessage()
      this.ele.appendChild(warning)
    }
  },
  methods: {
    init() {
      const width = this.ele.clientWidth
      const height = this.ele.clientHeight
      this.renderer = new WebGLRenderer({
        antialias : true // 是否弱化锯齿特性，true表示弱化，更消耗cpu
      })
      this.renderer.setSize(width, height)
      this.ele.appendChild(this.renderer.domElement)
      this.renderer.setClearColor(0xffffff, 1)

      this.camera = new PerspectiveCamera(45, width / height, 1, 10000)
      this.camera.position.set(600, 0, 600)
      this.camera.up.set(0, 1, 0)
      this.camera.lookAt(new Vector3(0, 0, 0))

      this.scene = new Scene()

      // AmbientLight，环境光，在场景的任何角度看到的效果都没有区别
      const light1 = new AmbientLight(0xff0000)
      light1.position.set(100, 100, 200)
      this.scene.add(light1)

      const geometry = new CubeGeometry(200, 100, 50, 4, 4)
      // 材质反射与光源相同的光
      const material = new MeshLambertMaterial({ color: 0xffff00 })
      const mesh = new Mesh(geometry, material)
      mesh.position.set(0, 0, 0)
      this.scene.add(mesh)

      this.renderer.render(this.scene, this.camera)
    }
  }
}
</script>